import sys
import os
sys.path.append("..")

import random
from CoreSim.Unit import *
from CoreSim.Simulator import *
from CoreSim.SchedulerHybrid import *

def MakeFlows(flowsFileName, numHosts, numLinks, numSwitches, trafficType):
    f = open(inDir + flowsFileName, "w")
    if trafficType == "random":
        for i in xrange(0, numSwitches):
            for j in xrange(0, numHosts):
                while (True):
                    i_remote = random.randint(0, numSwitches-1)
                    if (i_remote!=i):
                        break;
                j_remote = random.randint (0, numHosts-1)
    
                Host_id = numHosts*i+j+1;
                Host_remote_id = numHosts*i_remote+j_remote+1;
                print >> f, Host_id, Host_remote_id
    elif trafficType == "stride":
        stride = 6
        for i in xrange(0, numSwitches):
            for j in xrange(0, numHosts):
                while (True):
                    i_remote = random.randint(0, numSwitches-1)
                    if (i_remote!=i):
                        break;
                j_remote = random.randint (0, numHosts-1)
    
                Host_id = numHosts*i+j+1;
                Host_remote_id = (Host_id+stride)%(numHosts*numSwitches)+1;
                if Host_remote_id == Host_id:
                    Host_remote_id +=1
                print >> f, Host_id, Host_remote_id
    elif trafficType == "cluster":
        pass

    elif trafficType == "permutation":
        pass

    f.close()

if __name__ == "__main__":

    numHosts = 10
    numLinks = 10
    numStaticLinks = 1
    numSwitches = 64
    link_sh_Bw = 10.0 * Gb
    link_ss_Bw = 10.0 * Gb
    trafficType = 'random'
    schedId = 0
    sched_type = ['Jelly', 'Fully', 'Detour', 'OvS']

    flowsFileName = "Static/%s_%sToRs_%sHosts_%sLinks.txt" \
            %(trafficType, numSwitches,numHosts,numLinks)

    MakeFlows(flowsFileName, numHosts, numLinks, numSwitches, trafficType)
    
    sim = Simulator()
    SchedulerHybrid = SchedulerHybrid(numLinks, numStaticLinks, numHosts, numSwitches, link_ss_Bw, sched_type[schedId], k=2)
    sim.AssignSdnScheduler(Scheduler=SchedulerHybrid, args=(trafficType,flowsFileName))
    sim.sched.InitialTopology()
    sim.sched.CalBandwidth()
    sim.sched.PrintFlows()
    '''
    sim.sched.Reconfigure_network()
    sim.sched.CalBandwidth()
    sim.sched.PrintFlows()
    '''